package com.keqiang.chart.impl.axis

import androidx.annotation.IntRange
import kotlin.math.roundToInt

/**
 * 配置Y轴相关数据
 *
 * @author Created by wanggaowan on 2022/7/20 10:15
 */
open class YAxis : BaseYAxis() {
    /**
     * 绘制坐标起始值
     */
    open var min: Float = 0f

    /**
     * 绘制坐标结束值
     */
    open var max: Float = 100f

    /**
     * 轴上标签数量。比如设置标签数量为6，如果使用默认的ValueFormat,设置Y轴最小、最大值分别为0、100，则Y轴标签值为0，20，40，60，80，100
     */
    @setparam:IntRange(from = 1)
    override var labelCount: Int = super.labelCount
        set(value) {
            if (field == value) {
                return
            }

            field = if (value < 1) {
                1
            } else {
                value
            }
        }

    override var axisValueFormat: AxisValueFormat = object : AxisValueFormat {
        override fun invoke(position: Int): Label {
            if (labelCount <= 0) {
                return Label.obtain("")
            }

            if (labelCount == 1 || position == 0) {
                return Label.obtain(min.roundToInt().toString())
            }

            if (position == labelCount - 1) {
                return Label.obtain(max.roundToInt().toString())
            }

            val step = 0f.coerceAtLeast((max - min) / (labelCount - 1))
            return Label.obtain((min + (position * step)).roundToInt().toString())
        }
    }

    override fun apply(axis: Axis) {
        super.apply(axis)
        if (axis is YAxis) {
            this.min = axis.min
            this.max = axis.max
        }
    }
}

/**
 * 配置Y轴相关数据
 *
 * @author Created by wanggaowan on 2022/9/2 10:09
 */
open class YAxisHorizontal : BaseYAxis()